package com.hongyuan.coach.ui.home.view_mode;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.coach.R;
import com.hongyuan.coach.base.CustomViewModel;
import com.hongyuan.coach.base.ItemViewModel;
import com.hongyuan.coach.data.Constants;
import com.hongyuan.coach.data.MyRepository;
import com.hongyuan.coach.ui.home.activity.AppointmentStudentsActivity;
import com.hongyuan.coach.ui.home.beans.HomeContentBean;
import com.hongyuan.coach.ui.home.beans.TopTimeBean;
import com.hongyuan.coach.utils.TimeUtil;
import java.util.List;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class HomeViewMode extends CustomViewModel<MyRepository> {

    //选中项
    public ObservableField<TopTimeBean> selectBean = new ObservableField<>();

    public HomeViewMode(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<HomeTopItemViewModel> topList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<HomeTopItemViewModel> topBinding = ItemBinding.of(BR.viewModel, R.layout.item_home_top);

    //给RecyclerView添加ObservableList
    public ObservableList<HomeItemViewModel> contentList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<HomeItemViewModel> contentBinding = ItemBinding.of(BR.viewModel, R.layout.item_home_content);

    //预约学员
    public BindingCommand goAppointment = new BindingCommand(() -> startActivity(AppointmentStudentsActivity.class));


    @Override
    public void onResume() {
        super.onResume();

        if(selectBean.get() != null){
            getData();
        }
    }

    /*
     * 预约列表
     * 特殊字符处理：%20表示空格，%3A表示：
     * */
    private void getData(){

        clearParams().setParams("start_date", selectBean.get().getNow_day())
                .setParams("end_date", TimeUtil.getNextDay(1,selectBean.get().getNow_day(),TimeUtil.dateFormatYMD))
                .setParams("state_str","1");
        requestData(Constants.GET_COACH_APPOINTMENT_COURSE_PRIVITE_LIST);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_PLAN_DATA){
            List<TopTimeBean> mList = (List<TopTimeBean>) dataBean;

            topList.clear();

            for(int i = 0 ; i < mList.size() ; i++){
                HomeTopItemViewModel itemViewModel = new HomeTopItemViewModel(this,mList.get(i));
                topList.add(itemViewModel);

                if(i == 0){
                    mList.get(i).setSelect(true);
                    selectBean.set(mList.get(i));

                    this.itemViewModel = itemViewModel;
                }
            }

            //请求第一天的预约列表
            getData();
        }

        if(code == Constants.GET_COACH_APPOINTMENT_COURSE_PRIVITE_LIST){
            List<HomeContentBean.ListBean> list = ((HomeContentBean)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                contentList.clear();

                if(itemViewModel != null){
                    itemViewModel.entity.get().setSelect(true);
                    itemViewModel.entity.notifyChange();
                }
            }

            if(list != null && list.size() > 0){
                for(HomeContentBean.ListBean bean : list){
                    HomeItemViewModel itemViewModel = new HomeItemViewModel(this,bean);
                    contentList.add(itemViewModel);
                }
            }

            if(contentList.size() > 0){
                if(list == null || list.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }
        }


    }

    //当前操作的对象
    public HomeTopItemViewModel itemViewModel;

    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);
        this.itemViewModel = (HomeTopItemViewModel) itemVM;
        selectBean.set(itemViewModel.entity.get());
    }
}
